﻿using System;
using System.ComponentModel;
using System.Data.Linq;
using System.Data.Linq.Mapping;

namespace Data.MappingClasses
{
    [TableAttribute(Name = "dbo.TeacherSubject")]
    public partial class TeacherSubject : INotifyPropertyChanging, INotifyPropertyChanged
    {

        private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

        private short _idTeacherSubject;

        private short _refTeacher;

        private short _refSubject;

        private EntitySet<Group> _Groups;

        private EntityRef<Subject> _Subject;

        private EntityRef<Teacher> _Teacher;

        #region Extensibility Method Definitions
        partial void OnLoaded();
        partial void OnValidate(System.Data.Linq.ChangeAction action);
        partial void OnCreated();
        partial void OnidTeacherSubjectChanging(short value);
        partial void OnidTeacherSubjectChanged();
        partial void OnrefTeacherChanging(short value);
        partial void OnrefTeacherChanged();
        partial void OnrefSubjectChanging(short value);
        partial void OnrefSubjectChanged();
        #endregion

        public TeacherSubject()
        {
            this._Groups = new EntitySet<Group>(AttachGroups, DetachGroups);
            this._Subject = default(EntityRef<Subject>);
            this._Teacher = default(EntityRef<Teacher>);
            OnCreated();
        }

        [ColumnAttribute(Storage = "_idTeacherSubject", AutoSync = AutoSync.OnInsert, DbType = "SmallInt NOT NULL IDENTITY", IsPrimaryKey = true, IsDbGenerated = true)]
        public short idTeacherSubject
        {
            get
            {
                return this._idTeacherSubject;
            }
            set
            {
                if ((this._idTeacherSubject != value))
                {
                    this.OnidTeacherSubjectChanging(value);
                    this.SendPropertyChanging();
                    this._idTeacherSubject = value;
                    this.SendPropertyChanged("idTeacherSubject");
                    this.OnidTeacherSubjectChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_refTeacher", DbType = "SmallInt NOT NULL")]
        public short refTeacher
        {
            get
            {
                return this._refTeacher;
            }
            set
            {
                if ((this._refTeacher != value))
                {
                    if (this._Teacher.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnrefTeacherChanging(value);
                    this.SendPropertyChanging();
                    this._refTeacher = value;
                    this.SendPropertyChanged("refTeacher");
                    this.OnrefTeacherChanged();
                }
            }
        }

        [ColumnAttribute(Storage = "_refSubject", DbType = "SmallInt NOT NULL")]
        public short refSubject
        {
            get
            {
                return this._refSubject;
            }
            set
            {
                if ((this._refSubject != value))
                {
                    if (this._Subject.HasLoadedOrAssignedValue)
                    {
                        throw new System.Data.Linq.ForeignKeyReferenceAlreadyHasValueException();
                    }
                    this.OnrefSubjectChanging(value);
                    this.SendPropertyChanging();
                    this._refSubject = value;
                    this.SendPropertyChanged("refSubject");
                    this.OnrefSubjectChanged();
                }
            }
        }

        [AssociationAttribute(Name = "TeacherSubject_Group", Storage = "_Groups", ThisKey = "idTeacherSubject", OtherKey = "refTeacherSubject")]
        public EntitySet<Group> Groups
        {
            get
            {
                return this._Groups;
            }
            set
            {
                this._Groups.Assign(value);
            }
        }

        [AssociationAttribute(Name = "Subject_TeacherSubject", Storage = "_Subject", ThisKey = "refSubject", OtherKey = "idSubject", IsForeignKey = true)]
        public Subject Subject
        {
            get
            {
                return this._Subject.Entity;
            }
            set
            {
                Subject previousValue = this._Subject.Entity;
                if (((previousValue != value)
                     || (this._Subject.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Subject.Entity = null;
                        previousValue.TeacherSubjects.Remove(this);
                    }
                    this._Subject.Entity = value;
                    if ((value != null))
                    {
                        value.TeacherSubjects.Add(this);
                        this._refSubject = value.idSubject;
                    }
                    else
                    {
                        this._refSubject = default(short);
                    }
                    this.SendPropertyChanged("Subject");
                }
            }
        }

        [AssociationAttribute(Name = "Teacher_TeacherSubject", Storage = "_Teacher", ThisKey = "refTeacher", OtherKey = "idTeacher", IsForeignKey = true)]
        public Teacher Teacher
        {
            get
            {
                return this._Teacher.Entity;
            }
            set
            {
                Teacher previousValue = this._Teacher.Entity;
                if (((previousValue != value)
                     || (this._Teacher.HasLoadedOrAssignedValue == false)))
                {
                    this.SendPropertyChanging();
                    if ((previousValue != null))
                    {
                        this._Teacher.Entity = null;
                        previousValue.TeacherSubjects.Remove(this);
                    }
                    this._Teacher.Entity = value;
                    if ((value != null))
                    {
                        value.TeacherSubjects.Add(this);
                        this._refTeacher = value.idTeacher;
                    }
                    else
                    {
                        this._refTeacher = default(short);
                    }
                    this.SendPropertyChanged("Teacher");
                }
            }
        }

        public event PropertyChangingEventHandler PropertyChanging;

        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void SendPropertyChanging()
        {
            if ((this.PropertyChanging != null))
            {
                this.PropertyChanging(this, emptyChangingEventArgs);
            }
        }

        protected virtual void SendPropertyChanged(String propertyName)
        {
            if ((this.PropertyChanged != null))
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        private void AttachGroups(Group entity)
        {
            this.SendPropertyChanging();
            entity.TeacherSubject = this;
        }

        private void DetachGroups(Group entity)
        {
            this.SendPropertyChanging();
            entity.TeacherSubject = null;
        }
    }
}